package org.beian.fmmall.service.Impl;

import org.beian.fmmall.VO.ResStatus;
import org.beian.fmmall.VO.ResultVO;
import org.beian.fmmall.beans.ProductComments;
import org.beian.fmmall.beans.ProductCommentsVO;
import org.beian.fmmall.dao.ProductCommentsMapper;
import org.beian.fmmall.service.ProductCommentsService;
import org.beian.fmmall.utils.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
@Service
public class ProductCommentsServiceImpl implements ProductCommentsService {
    @Autowired
    private ProductCommentsMapper productCommentsMapper;
    @Override
    public ResultVO listCommentsByProductId(String productId,int pageNum,int limit) {
        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
        int count = productCommentsMapper.selectCountByExample(example);
        int pageCount=count%limit==0?count/limit:count/limit+1;
        int start=(pageNum-1)*limit;
        List<ProductCommentsVO> list = productCommentsMapper.selectCommentsByProductId(productId, start, limit);
        return new ResultVO(ResStatus.OK,"success",new PageHelper<ProductCommentsVO>(count,pageCount,list));
    }

    @Override
    public ResultVO getCommentsCountByProductId(String productId) {
        //总评论数
        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
        int total=productCommentsMapper.selectCountByExample(example);
        //好评数
        Example example1 = new Example(ProductComments.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("productId",productId);
        criteria1.andEqualTo("commType",1);
        int goodTotal=productCommentsMapper.selectCountByExample(example1);
        //中评数
        Example example2 = new Example(ProductComments.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("productId",productId);
        criteria2.andEqualTo("commType",0);
        int midTotal=productCommentsMapper.selectCountByExample(example2);
        //差评数
        Example example3 = new Example(ProductComments.class);
        Example.Criteria criteria3 = example3.createCriteria();
        criteria3.andEqualTo("productId",productId);
        criteria3.andEqualTo("commType",-1);
        int badTotal=productCommentsMapper.selectCountByExample(example3);
        //好评率
        double result=(double)goodTotal/total;
        String goodRate=String.format("%.2f",result*100);
        HashMap<String,Object> map=new HashMap<>();
        map.put("total",total);
        map.put("goodTotal",goodTotal);
        map.put("midTotal",midTotal);
        map.put("badTotal",badTotal);
        map.put("goodRate",goodRate);
        return new ResultVO(ResStatus.OK,"success",map);
    }
}
